package top.huxy.boot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.BufferedImageHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import top.huxy.boot.config.argumentResolver.AdminArgumentResolver;
import top.huxy.boot.config.argumentResolver.PagerArgumentResolver;
import top.huxy.boot.config.argumentResolver.QueryArgumentResolver;
import top.huxy.boot.config.argumentResolver.UserArgumentResolver;
import top.huxy.boot.config.interceptor.AdminControllerInterceptor;
import top.huxy.boot.config.interceptor.CrossDomainInterceptor;
import top.huxy.boot.config.interceptor.ThreadLocalInitializeInterceptor;
import top.huxy.boot.config.interceptor.ThreadLocalTerminateInterceptor;
import top.huxy.boot.config.messageConverter.BinaryResultMessageConverter;
import top.huxy.boot.config.messageConverter.ResultMessageConverter;

import java.util.List;

/**
 * 设置web一些配置
 */
@Configuration
@Import({
        AdminArgumentResolver.class,
        PagerArgumentResolver.class,
        UserArgumentResolver.class,
        QueryArgumentResolver.class,
        AdminControllerInterceptor.class,
        CrossDomainInterceptor.class,
        ThreadLocalInitializeInterceptor.class,
        ThreadLocalTerminateInterceptor.class
})
public class WebConfiguration extends WebMvcConfigurationSupport{

    /**
     * 控制器中获取到Admin参数
     */
    @Autowired
    public AdminArgumentResolver adminArgumentResolver;

    /**
     * 分页场景控制器下可以获取到分页对象
     */
    @Autowired
    public PagerArgumentResolver pagerArgumentResolver;

    /**
     * 控制器中科院获取到User
     */
    @Autowired
    public UserArgumentResolver userArgumentResolver;

    /**
     * 筛选场景控制器下可以获取到Query对象
     */
    @Autowired
    public QueryArgumentResolver queryArgumentResolver;

    /**
     * admin访问拦截
     */
    @Autowired
    public AdminControllerInterceptor adminControllerInterceptor;

    /**
     * 跨域请求拦截器
     */
    @Autowired
    public CrossDomainInterceptor crossDomainInterceptor;

    /**
     * ThreadLocal变量初始化器
     */
    @Autowired
    public ThreadLocalInitializeInterceptor threadLocalInitializeInterceptor;

    /**
     * ThreadLocal变量销毁器
     */
    @Autowired
    public ThreadLocalTerminateInterceptor threadLocalTerminateInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //允许跨域请求。必须放第一位
        registry.addInterceptor(crossDomainInterceptor);

        //ThreadLocal变量初始化器，必须放第二位
        registry.addInterceptor(threadLocalInitializeInterceptor);

        //Admin接口拦截
        registry.addInterceptor(adminControllerInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/admin/login", "/admin/admin/autoLogin");

        //销毁ThreadLocal变量，必须放最后
        registry.addInterceptor(threadLocalTerminateInterceptor);
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //BufferedImage返回值处理
        converters.add(0, new BufferedImageHttpMessageConverter());
        //Result返回值处理
        converters.add(0, new ResultMessageConverter());
        //binaryResult的返回值处理
        converters.add(0, new BinaryResultMessageConverter());
    }

    @Override
    protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        //Admin 参数
        argumentResolvers.add(adminArgumentResolver);

        //Pager 参数
        argumentResolvers.add(pagerArgumentResolver);

        //User 参数
        argumentResolvers.add(userArgumentResolver);

        //Query参数
        argumentResolvers.add(queryArgumentResolver);
    }
}
